package com.bluefoxyu.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.bluefoxyu.domain.dto.AdminDTO;
import com.bluefoxyu.domain.dto.StudentAddDTO;
import com.bluefoxyu.domain.entity.Admin;
import com.bluefoxyu.domain.entity.Enrollment;
import com.bluefoxyu.domain.entity.Student;
import com.bluefoxyu.domain.vo.AdminVO;
import com.bluefoxyu.exception.WorkException;
import com.bluefoxyu.mapper.AdminMapper;
import com.bluefoxyu.mapper.EnrollmentMapper;
import com.bluefoxyu.mapper.StudentMapper;
import com.bluefoxyu.properties.JwtClaimsConstant;
import com.bluefoxyu.properties.JwtProperties;
import com.bluefoxyu.service.IAdminService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bluefoxyu.utils.JwtUtil;
import jakarta.annotation.Resource;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 管理员信息表 服务实现类
 * </p>
 *
 * @author bluefoxyu
 * @since 2024-06-16
 */
@Service
public class AdminServiceImpl extends ServiceImpl<AdminMapper, Admin> implements IAdminService {

    @Resource
    private AdminMapper adminMapper;
    @Resource
    private JwtProperties jwtProperties;
    @Resource
    private StudentMapper studentMapper;
    @Resource
    private EnrollmentMapper enrollmentMapper;


    @Override
    public AdminVO login(AdminDTO adminDTO) {

        String username= adminDTO.getUsername();
        String password= adminDTO.getPassword();

        // 根据用户名查询数据库
        QueryWrapper<Admin> wrapper=new QueryWrapper<>();
        wrapper.lambda()
                .eq(Admin::getUsername,username);
        Admin admin = adminMapper.selectOne(wrapper);

        // 判断查询结果是否为null,根据名字查询如果账户里面没有那个人则退出查询
        // 如果为null，则抛出AccountNotFoundException异常，并返回错误信息
        if (admin == null) {
            throw new WorkException("账号不存在");
        }
        // 判断密码是否正确，如果密码不正确，则抛出异常，并返回错误信息
        //密码比对
        boolean flag= password.equals(admin.getPassword());
        if (!flag) {
            throw new WorkException("密码错误");
        }

        //登录成功后返回jwt令牌
        Map<String,Object> claims=new HashMap<>();
        claims.put(JwtClaimsConstant.ADMIN_ID,admin.getId());

        //生成token，注入相关jwtProperties配置属性
        String token= JwtUtil.createJWT(
                jwtProperties.getAdminSecretKey(),
                jwtProperties.getAdminTtl(),
                claims);

        // 返回token
        return AdminVO.builder()
                .id(admin.getId())
                .username(admin.getUsername())
                .token(token).build();

    }

    @Override
    public void addStudent(StudentAddDTO studentAddDTO) {
        Student student=new Student();
        BeanUtils.copyProperties(studentAddDTO,student);
        //设置初始密码为123456
        student.setPassword("123456");
        studentMapper.insert(student);
    }

    @Override
    public void deleteByIds(List<Integer> ids) {

        //先删除成绩单中学生信息
        for (Integer studentId : ids) {
            QueryWrapper<Enrollment> wrapper=new QueryWrapper<>();
            wrapper.lambda().eq(Enrollment::getStudentId,studentId);
            enrollmentMapper.delete(wrapper);
        }

        //再删除学生信息
        studentMapper.deleteBatchIds(ids);
    }

}
